function getTop(i){
     return cellSidePadding+i*(cellSideLength+cellSidePadding);
}

function getLeft(j){
    return cellSidePadding+j*(cellSideLength+cellSidePadding);
}

function getNumBgColor(num){
    switch(num){
	    case 2: return '#930'; break;
		case 4: return '#999933'; break;
		case 8: return '#CC3300'; break;
		case 16: return '#CC9999'; break;
		case 32: return '#FF9966'; break;
		case 64: return '#FF6600'; break;
		case 128: return '#669933'; break;
		case 256: return '#9933FF'; break;
		case 512: return '#663399'; break;
		case 1024: return '#00CC66'; break;
		case 2048: return '#FF66FF'; break;
		case 4096: return '#CC3300'; break;
		case 8192: return '#669900'; break;
	}
}

function getNumColor(num){
    if(num<4){
	      return '#776e65';
	}
	return 'white';
}

function noPlace(gride){
    for(var i=0; i<4; i++){
	     for(var j=0; j<4;j++){
		     if(gride[i][j]==0){
			       return false;
			 }
		 }
	}
	return true;
}

function updateNum(){
	$('.gride-cell').remove();
	for(var i=0; i<4; i++){
	     for(var j=0; j<4;j++){
		      var element=$('.num-div');
			  element.append('<div class="gride-cell" id="gride-cell-'+i+'-'+j+'"></div>');
			  var activeEle=$('#gride-cell-'+i+'-'+j);
			  if(gride[i][j]==0){
			       activeEle.css('width',0);
				   activeEle.css('height',0);
				   activeEle.css('top',getTop(i)+cellSideLength/2);
				   activeEle.css('left',getLeft(j)+cellSideLength/2);
			  }else{
			       activeEle.css('width',cellSideLength);
				   activeEle.css('height',cellSideLength);
				   activeEle.css('top',getTop(i));
				   activeEle.css('left',getLeft(j));
				   activeEle.css('background-color',getNumBgColor(gride[i][j]));
				   activeEle.css('color',getNumColor(gride[i][j]));
				   activeEle.text(gride[i][j]);
			  }
			  hasConfig[i][j]=false;
		 }
	}
	
	$('.gride-cell').css({
	      'line-height':cellSideLength+'px',
		  'font-size':0.6*cellSideLength,
		  'border-radius':0.1*cellSideLength
	})

}
function canMoveLeft(gride){
    for(var i=0; i<4; i++){
	      for(var j=1; j<4 ; j++){
		        if(gride[i][j]!=0){
				     if(gride[i][j-1]==0 || gride[i][j-1]==gride[i][j]){
					      return true;
					 }
				}
		  }
	}
	return false;
}

function pointNoThing(row,coll1,coll2,gride){
	for(var i=coll1+1; i<coll2; i++){
	     if(gride[row][i]!=0){
		     return false;
		 }
	}
	return true;  
}

function canMoveTop(){
    for(var i=1; i<4;i++)
	    for(var j=0; j<4;j++)
		    if(gride[i][j]!=0)
			    if(gride[i-1][j]==0|| gride[i-1][j]==gride[i][j])
				   return true;
	return false;
}

function pointTopNoThing(row,row1,coll){
    for(var i=row1+1; i<row; i++)
	    if(gride[i][coll]!=0)
		    return false;
	return true;
}

function canMoveLeft(){
    for(var i=0; i<4;i++)
	    for(var j=0; j<3; j++)
		    if(gride[i][j]!=0)
			    if(gride[i][j+1]==0|| gride[i][j+1]==gride[i][j])
				     return true;
	return false;
}

function pointRightNoThing(row,coll1,coll2){
    for( var i=coll2+1; i<coll1;i++){
	     if(gride[row][i]!=0)
		      return false;
	}
	return true;
}

function canMoveBottom(){
    for(var i=0; i<3; i++)
	   for(var j=0; j<4; j++)
	       if(gride[i][j]!=0)
		       if(gride[i+1][j]==0||gride[i+1][j]==gride[i][j])
			       return true;
	return false;
}

function pointBottomNoThing(row,row1,coll){
      for(var i=row+1; i<row1; i++){
	       if(gride[i][coll]!=0){
			      return false;
			}
	  }
	  return true;
}